Skip to content

File Processing

Alt text

File processing operations

  • Files are frequently used to store records that include data types other than string.

  • Also, many programs need to handle random access files so that a record can be found quickly without reading through all the preceding records.

  • A typical record to be stored in a file could be declared like this in pseudocode:

TYPE TstudentRecord
  DECLARE name : STRING
  DECLARE registerNumber : INTEGER
  DECLARE dateOfBirth :  DATE
  DECLARE fullTime : BOOLEAN
ENDTYPE

Storing records in a serial or sequential file

  • The algorithm to store records sequentially in a serial (unordered) or sequential (ordered on a key field) file is very similar to the algorithm for storing lines of text in a text file.
  • The algorithm written in pseudocode below stores the student records sequentially in a serial file as they are input.
  • Note that PUTRECORD is the pseudocode to write a record to a data file and GETRECORD is the pseudocode to read a record from a data file.
DECLARE studentRecord : ARRAY[1:50] OF TstudentRecord
DECLARE studentFile : STRING
DECLARE counter : INTEGER
counter ← 1
studentFile ← "studentFile.dat"
OPEN studentFile FOR WRITE
REPEAT
  OUTPUT "Please enter student name"
  INPUT studentRecord.name[counter]
  IF studentRecord.name <> "" THEN
    PUTRECORD, studentRecord[counter]
    counter ← counter + 1
  ELSE
    CLOSEFILE(studentFile)
  ENDIF
UNTIL studentRecord.name = ""

OUTPUT "The file contains these records:"
OPEN studentFile FOR READ
counter ← 1
REPEAT
  GETRECORD, studentRecord[counter]
  OUTPUT studentRecord[counter]
  counter ← counter + 1
UNTIL EOF(studentFile)
CLOSEFILE(studentFile)

Adding a record to a sequential file

  • Records can be appended to the end of a serial file by opening the file in append mode.
  • If records need to be added to a sequential file, then the whole file needs to be recreated and the record stored in the correct place.
DECLARE studentRecord : TstudentRecord
DECLARE newStudentRecord : TstudentRecord
DECLARE studentFile : STRING
DECLARE newStudentFile : STRING
DECLARE recordAddedFlag : BOOLEAN
recordAddedFlag ← FALSE
studentFile ← "studentFile.dat"
newStudentFile ← "newStudentFile.dat"
CREATE newStudentFile // creates a new file to write to
OPEN newStudentFile FOR WRITE
OPEN studentFile FOR READ
OUTPUT "Please enter student details"
OUTPUT "Please enter student name"
INPUT newStudentRecord.name
OUTPUT "Please enter student’s register number"
INPUT newStudentRecord.registerNumber
OUTPUT "Please enter student’s date of birth"
INPUT newStudentRecord.dateOfBirth
OUTPUT "Please enter True for full-time or False for part-time"
INPUT newStudentRecord.fullTime
REPEAT
    WHILE NOT recordAddedFlag OR EOF(studentFile)
        GETRECORD, studentRecord // gets record from existing file
        IF newStudentRecord.registerNumber > studentRecord.registerNumber THEN
            PUTRECORD studentRecord
            // writes record from existing file to new file
        ELSE
            PUTRECORD newStudentRecord
            // or writes new record to new file in the correct place record
            AddedFlag ← TRUE
        ENDIF
    ENDWHILE
IF EOF (studentFile) THEN
    PUTRECORD newStudentRecord
    // add new record at end of the new file
ELSE
    REPEAT
        GETRECORD, studentRecord
        PUTRECORD studentRecord
        //transfers all remaining records to the new file
    UNTIL EOF(studentRecord)
ENDIF
CLOSEFILE(studentFile)
CLOSEFILE(newStudentFile)
DELETE(studentFile)
// deletes old file of student records
RENAME newStudentfile, studentfile
// renames new file to be the student record file

Adding a record to a random file

  • Records can be added to a random file by using a hashing function on the key field of the record to be added.
  • The hashing function returns a pointer to the address where the record is to be added.

In pseudocode, the address in the file can be found using the command:

SEEK <filename>,<address>

The record can be stored in the file using the command:

PUTRECORD <filename>,<recordname>

Or it can be retrieved using:

GETRECORD <filename>,<recordname>

The file needs to be opened as random:

OPEN studentFile FOR RANDOM

DECLARE studentRecord : TstudentRecord
DECLARE studentFile : STRING
DECLARE Address : INTEGER
studentFile ← "studentFile.dat"
OPEN studentFile FOR RANDOM
// opens file for random access both read and write
OUTPUT "Please enter student details"
OUTPUT "Please enter student name"
INPUT StudentRecord.name
OUTPUT "Please enter student’s register number"
INPUT studentRecord.registerNumber
OUTPUT "Please enter student’s date of birth"
INPUT studentRecord.dateOfBirth
OUTPUT "Please enter True for full-time or False for part-time"
INPUT studentRecord.fullTime
address ← hash(studentRecord,registerNumber)// uses function hash to find pointer to address
SEEK studentFile,address // finds address in file
PUTRECORD studentFile,studentRecord //writes record to the file
CLOSEFILE(studentFile)

Finding a record in a random file

  • Records can be found in a random file by using a hashing function on the key field of the record to be found.
DECLARE studentRecord : TstudentRecord DECLARE studentFile : STRING
DECLARE Address : INTEGER
studentFile ← "studentFile.dat"
OPEN studentFile FOR RANDOM // opens file for random access both read and write
OUTPUT "Please enter student’s register number"
INPUT studentRecord.registerNumber
address ← hash(studentRecord.registerNumber) // uses function hash to find pointer to address
SEEK studentFile,address // finds address in file
GETRECORD studentFile,studentRecord //reads record from the file
OUTPUT studentRecord
CLOSEFILE(studentFile)

Exception handling

  • An exception is an unexpected event that disrupts the execution of a program.

  • Exception handling is the process of responding to an exception within the program so that the program does not halt unexpectedly.

  • Exception handling makes a program more robust as the exception routine traps the error then outputs an error message, which is followed by either an orderly shutdown of the program or recovery if possible.

  • An exception may occur in many different ways, for example

    • dividing by zero during a calculation
    • reaching the end of a file unexpectedly when trying to read a record from a file
    • trying to open a file that has not been created
    • losing a connection to another device, such as a printer.
//Pseudocode
//Exception handling
TRY
  <statements>
EXCEPT
  <statements>
ENDTRY
py
#Python Exception handling
def division(firstNumber, secondNumber):
  try:
    myAnswer = firstNumber // secondNumber
    print('Answer ', myAnswer)
  except:
    print('Divide by zero')
division(12, 3)
division(10, 0)